package com.hu.common.util.help;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hu.common.constants.Constants;
import com.hu.common.domain.base.UserVO;
import com.hu.common.enums.RoleEnum;
import com.hu.common.util.CurrentUserUtil;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 00351634
 * @version 1.0
 * @date 2024/7/17 10:59
 * @description: 数据操作辅助类
 */
public class DataOperateHelper {
    /**
     * 判断是否可管理改数据
     *
     * @param roleList
     * @param staffCodeList
     * @return
     */
    public static int isAdmin(List<RoleEnum> roleList, List<String> staffCodeList) {
        UserVO userVo = CurrentUserUtil.getCurrentUser();
        if (ObjectUtil.isNull(userVo)) {
            return Constants.EditState.NO;
        }
        // 是否是业务管理员
        boolean isBizAdmin = CollectionUtil.isNotEmpty(staffCodeList) && staffCodeList.contains(userVo.getUsername());
        return isBizAdmin || isAdminRole(roleList) ? Constants.EditState.YES : Constants.EditState.NO;
    }

    /**
     * @param roleList
     * @return
     */
    public static int canTop(List<RoleEnum> roleList) {
        return isAdminRole(roleList) ? Constants.EditState.YES : Constants.EditState.NO;
    }

    public static int withDrawAdmin(List<RoleEnum> roleList) {
        return isAdminRole(roleList) ? Constants.EditState.YES : Constants.EditState.NO;
    }

    private static boolean isAdminRole(List<RoleEnum> roleList) {
        UserVO userVo = CurrentUserUtil.getCurrentUser();
        if (ObjectUtil.isNull(userVo)) {
            return false;
        }
        if (CollectionUtil.isNotEmpty(roleList)) {
            List<String> roleCodeList = roleList.stream().map(RoleEnum::getCode).collect(Collectors.toList());
            // 当前用户角色列表与业务管理角色列表取交集，判断是否有编辑权限
            Collection<String> intersection = CollectionUtil.intersection(roleCodeList, userVo.getRoleCodes());
            return CollectionUtil.isNotEmpty(intersection);
        }
        return false;
    }
}
